package com.milkshopbackend.milkshop.controller;

import com.milkshopbackend.milkshop.Service.UserService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.*;
import com.milkshopbackend.milkshop.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<?> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        return Result.success(userService.register(registerDTO));
    }

    @PostMapping("/login")
    public Result<?> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        return Result.success(token);
    }

    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        String avatarUrl = userService.uploadAvatar(file);
        return Result.success(avatarUrl);
    }

    @PutMapping("/profile")
    public Result<?> updateProfile(@RequestBody UserProfileUpdateDTO profileDTO) {
        userService.updateProfile(profileDTO);
        return Result.success();
    }

    @GetMapping("/profile")
    public Result<?> getProfile() {
        return Result.success(userService.getById(SecurityUtils.getCurrentUserId()));
    }

    @PutMapping("/password")
    public Result<?> updatePassword(@RequestBody UserPasswordUpdateDTO dto) {
        userService.updatePassword(dto);
        return Result.success();
    }

    @PostMapping("/send-reset-code")
    public Result<?> sendResetCode(@RequestBody java.util.Map<String, String> body) {
        userService.sendResetCode(body.get("email"));
        return Result.success();
    }

    @PostMapping("/reset-password")
    public Result<?> resetPassword(@RequestBody UserResetPasswordDTO dto) {
        userService.resetPassword(dto);
        return Result.success();
    }
}
